/* 消息提示相关状态的Store */

import { defineStore } from 'pinia'

export const useAlertStore = defineStore('alert', {
	state: () => ({
		alerts: [],
		count: 0
	}),
	actions: {
		alert(type, message) {
			const alertObj = {
				id: this.count++,
				type: type, // 对应<el-alert/>的type属性
				title: message,
				show: false
			}
			this.alerts.push(alertObj)
			// 一定时间后显示
			setTimeout(() => {
				const index = this.alerts.indexOf(alertObj)
				this.alerts[index].show = true
			}, 1)
			// 一定时间后隐藏
			setTimeout(() => {
				const index = this.alerts.indexOf(alertObj)
				this.alerts[index].show = false
			}, 4000)
			// 一定时间后删除
			setTimeout(() => {
				const index = this.alerts.indexOf(alertObj)
				this.alerts.splice(index, 1)
			}, 6000)
		}
	}
})